/**
 * 微信SDK封装类
 */
export class WXSDK {
    private static instance: WXSDK = null;
    
    /**
     * 获取单例实例
     */
    public static getInstance(): WXSDK {
        if (!this.instance) {
            this.instance = new WXSDK();
        }
        return this.instance;
    }

    private loginPromiseResolve: ((value: string) => void) | null = null;

    /**
     * 微信登录
     * @returns Promise<string> 返回登录code
     */
    public login(): Promise<string> {
        return new Promise((resolve, reject) => {
            if (CC_DEV) {
                console.log('微信登录');
                resolve('debug_code');
                return;
            }

            this.loginPromiseResolve = resolve;
            jsb.reflection.callStaticMethod(
                "com/cocos/game/AppActivity",
                "wxLogin",
                "()V"
            );
          
        });
    }
    

    /**
     * 微信登录回调方法
     * @param code 微信登录返回的code
     */
    public onWXLoginCallback(code: string) {
        if (this.loginPromiseResolve) {
            this.loginPromiseResolve(code);
            this.loginPromiseResolve = null;
        }
    }

    /**
     * 分享到微信
     * @param title 标题
     * @param description 描述
     * @param url 链接
     * @param scene 场景 0:会话 1:朋友圈
     */
    public share(title: string, description: string, url: string, scene: number = 0): void {
        // 参数验证
        if (!title || !description || !url) {
            console.error('分享参数不能为空');
            return;
        }

        if (scene !== 0 && scene !== 1) {
            console.error('无效的分享场景');
            return;
        }

        if (CC_DEBUG) {
            console.log('分享到微信:', { title, description, url, scene });
            return;
        }

        jsb.reflection.callStaticMethod(
            "com/cocos/game/AppActivity",
            "shareToWeChat",
            "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V",
            title,
            description,
            url,
            scene
        );
    }
}